
import {getDetails} from '@/api/admin/user'

const validateUsername = (rule, value, callback) => {
  if (!value) {
    return callback(new Error('请输入用户名'))
  }
  getDetails(value).then(response => {
    if (window.boxType === 'edit') callback()
    let result = response.data.data
    if (result !== null) {
      callback(new Error('用户名已经存在'))
    } else {
      callback()
    }
  })
}

// 设置密码校验规则
const checkPassword = (rule, value, callback) => {
  if (window.boxType === 'edit') {
    return callback()
  }
  if (!value) {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

// 设置手机号的验证规则
const checkPhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入联系方式'))
  } else {
    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
    if (reg.test(value)) {
      callback()
    } else {
      return callback(new Error('请输入正确的电话'))
    }
  }
}

export const tableOption = {
  searchShow: true,
  searchMenuSpan: 6,
  stripe: true,
  headerAlign: 'center',
  tip: false,
  tree: true,
  border: true,
  index: true,
  indexLabel: '序号',
  selection: true,
  align: 'center',
  viewBtn: false,
  column: [{
    fixed: true,
    label: 'id',
    prop: 'id',
    span: 24,
    hide: true,
    editDisplay: false,
    addDisplay: false,
    viewDisplay: false
  }, {
    fixed: true,
    label: '用户名',
    prop: 'username',
    editDisabled: true,
    search: true,
    span: 24,
    rules: [{
      required: true,
      message: '请输入用户名'
    },
      {
        min: 3,
        max: 20,
        message: '长度在 3 到 20 个字符',
        trigger: 'blur'
      },
      {validator: validateUsername, trigger: 'blur'}
    ]
  },
    {
      label: '租户',
      prop: 'tenantId',
      type: "select",
      dicUrl: "/admin/tenant/list",
      props: {
        label: "name",
        value: "id"
      },
      span: 24,
      rules: [{
        required: true,
        message: '密码不能为空',
        trigger: 'blur'
      }]
    },
    {
    label: '密码',
    prop: 'password',
    type: 'password',
    value: '',
    hide: true,
    span: 24,
    viewDisplay: false,
    editDisplay: false,
    rules: [{validator: checkPassword, trigger: 'blur'}]
  }, {
    width: 100,
    label: '手机号',
    prop: 'phone',
    value: '',
    search: true,
    span: 24,
    rules: [{
      required: true,
      message: '密码不能为空',
      trigger: 'blur'
    }, {
      validator: checkPhone,
      trigger: 'blur'
    }]
  }, {
    width: 100,
    label: '角色',
    prop: 'role',
    type: "tree",
    multiple: true,
    span: 24,
    dicData: [],
    props: {
      label: "roleName",
      value: "id",
    },
    slot: true,
    rules: [{
      required: true,
      message: "请选择所属角色",
      trigger: "click"
    }]
  }, {
    label: '所属部门',
    prop: 'deptId',
    type: "tree",
    span: 24,
    dicData: [],
    props: {
      label: "name",
      value: "id",
    },
    slot: true,
    rules: [{
      required: true,
      message: "请选择所属部门",
      trigger: "click"
    }]
  }, {
    label: '状态',
    prop: 'lockFlag',
    type: 'radio',
    slot: true,
    border: true,
    span: 24,
    rules: [{
      required: true,
      message: '请选择状态',
      trigger: 'blur'
    }],
    dicUrl: '/admin/dict/type/lock_flag',
  }, {
    width: 120,
    label: '创建时间',
    prop: 'createTime',
    type: 'datetime',
    format: 'yyyy-MM-dd',
    editDisabled: true,
    addDisplay: false,
    span: 24
  }]
}
